<?php


namespace app\admin\service;


use app\admin\model\Registered;
use Page\Find;
use Page\Page;

class RegisteredService
{
    # 获取所有为借款记录
    public function getAllNoMeney($page, $limit)
    {
        $result = $this->get_layui_page($page, $limit);
        return $result;
    }

    # 获取所有的记录并分页
    private function get_layui_page($page, $limit)
    {
        $model = new Registered();
        $where = [
            ['loan_status', 'in', config('status.no')]
        ];
        $page = new Page($model, $page, $limit, [], null, $where);
        return $page->layuiPage();
    }

    # 搜索没有贷款的记录
    public function findAll($param)
    {
        $where = $this->get_where($param);
        $find = new Find();
        $model = new Registered();
        $param['field']['loan_status'] = 1;
        $result = $find->find($model, $param, $where);
        return $result;
    }

    # 根据参数组装条件
    private function get_where($param)
    {
        $filed = $param['field'];
        $time = $filed['time'];

        if(!strstr($time,'.')){
            $time_start = '';
            $time_end = '';
        }else {
            $date = explode('.', $filed['time']);
            $time_start = $date[0];
            $time_end = $date[1];
        }

        $where = [
            'address' => ['address','like','%'.$filed['address'].'%'],
            'ip' => ['ip','like','%'.$filed['ip'].'%'],
            'from' => ['from','=',$filed['from']],
            'signData' => ['sign','=',$filed['signData']],
            'time' => ['add_time', 'between time', [$time_start, $time_end]],
            'phone'=> ['phone', '=', $filed['phone']],
            'type'=> ['type', '=', $filed['type']],
            'first'=> ['first', '=', $filed['first']],
            'loan_status' => ['loan_status', 'in', config('status.no')]
        ];
        return $where;
    }
}